#!/usr/bin/env python

import sys

try:
   import pygtk
   pygtk.require("2.0")
except:
   pass
try:
   import gtk
   import gtk.glade
except:
   sys.exit(1)

class pyLengHandlers:
   """Leng's Library UI Event Handlers"""

   def __init__(self, widgetry):
      self.widgetry = widgetry

      # Connect a menu to the Leng button
      lengMenu = self.widgetry.get_widget("menuLengButton")
      lengButton = self.widgetry.get_widget("menutoolbuttonLengButton")
      lengButton.set_menu(lengMenu)

   def on_lengButton_clicked(self, widget, data=None):
      lengMenu = self.widgetry.get_widget("menuLengButton")
      lengMenu.popup(None, None, self.position_lengMenu, gtk.gdk.BUTTON1_MASK, 0, None)

   def position_lengMenu(self, menu, data):
      lengButton = self.widgetry.get_widget("menutoolbuttonLengButton")
      lengWindow = lengButton.get_parent_window()
      origin = lengWindow.get_origin()
      rect = lengButton.get_allocation()
      return (origin[0] + rect.x, origin[1] + rect.y + rect.height, True)

   def on_About_activate(self, widget, data=None):
      lengAboutDialog = self.widgetry.get_widget("aboutLengDialog")
      lengAboutDialog.run()

   def on_winMain_close(self, widget, event, data=None):
      return on_winMain_end(self, widget, data)

   def on_winMain_end(self, widget, data=None):
      gtk.main_quit()
      return False

